<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %><%--
  Created by IntelliJ IDEA.
  User: 给
  Date: 2020/10/7
  Time: 9:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%
  //修改request中的数据编码
  request .setCharacterEncoding("utf-8");
  String username=request .getParameter("username");
  String password=request .getParameter("password");

  //加载数据库驱动
  Class.forName("com.mysql.jdbc.Driver");

  //建立数据库连接
  String url="jdbc:mysql://localhost:3306/book";
  Connection  connection = DriverManager .getConnection(url,"root","root");

  //sql 模板
  String sql="select * from user where username=? and password=?";
  //使用PreparedStatement 对象执行sql语句
  PreparedStatement  ps=connection .prepareStatement(sql);

  ps.setString(1.username);
  ps.setString(2.password);

  ResultSet  rs=ps.executeQuery() ;
  
  if(rs.next()){
    //表示登录成功
    session .setAttribute("username",username );
    response .sendRedirect("home.jsp");
    else{
      //表示登录未成功
      out.print("登录失败，3秒后跳转登录页面。。。");
      response .sendRedirect("refresh","3;url=login.jsp");
    }
  }
%>